package com.fbli.controller;

import com.fbli.entity.Code;
import com.fbli.entity.Result;
import com.fbli.entity.SendMail;
import com.fbli.pojo.Users;
import com.fbli.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Timer;
import java.util.TimerTask;

@RestController
@RequestMapping("/user")
public class UserController {
    long yzm=0;
    @Autowired
    private UserService userService;
    @RequestMapping("/findByUserId")
    public Users findByUserId(int userId){
        Users user = userService.findByUserId(userId);

            System.out.println(user.getUsername());

        return user;
    }
    /*@RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Users user, HttpSession session, Model model){
        Users user1 = userService.login(user);

        if (user.getUsername()==null||user.getUsername().length()==0){
            System.out.println("false");
            Result result = new Result(false, "用户名和密码不能为空");
            model.addAttribute(result);
            return "index";
        }
        else if(user1.getUsername().equals(user.getUsername())&&user1.getPassword().equals(user.getPassword())){
            System.out.println("true");
            session.setAttribute("username",user1.getUsername());
            Result result = new Result(true, "登录成功");
            model.addAttribute(result);
            return "redirect:index";
        }else {
            System.out.println("false");
            Result result = new Result(false, "用户名或密码错误");
            model.addAttribute(result);
            return "index";
        }
    }*/
    @RequestMapping("/register")
    public Result register(@RequestBody Users user,@RequestParam String code,HttpSession session){
        try {
            System.out.println(code);
            //判断验证码是否过期
            if (session.getAttribute("yzm")!=null){
                //判断验证码是否正确
                String yzm1=yzm+"";
                if(code!=null&&yzm1.equals(code)){
                    Users users = userService.findOne(user.getUsername());
                    //判断用户名是否存在
                    if (users!=null&&user.getUsername().equals(users.getUsername())){
                        return new Result(false,"用户名已存在");
                    }else {
                        userService.register(user);
                        System.out.println("1");
                        return new Result(true,"注册成功");
                    }
                }else {
                    return new Result(false,"验证码不正确");
                }

            }else {
                return new Result(false,"验证码已过期");
            }

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"系统异常,注册失败");
        }
    }
    @RequestMapping("/selectPassword")
    public Result selectPassword(@RequestBody Users user){
        // 根据页面获取到的邮箱找到该用户信息
        Users users = userService.selectPassword(user.getEmail());
        // 实例化一个发送邮件的对象
        SendMail mySendMail = new SendMail();
        // 如果查到该用户，并且用户名和页面输入相同验证成功，发送邮件
        if (users!=null&&users.getUsername().equals(user.getUsername())){
            mySendMail.setUsername(users.getUsername());
            mySendMail.sendMail(user.getEmail(), "三丸医疗系统提醒，您的密码为：" + users.getPassword());
            System.out.println(users.getPassword()+"--"+users.getUsername());
            return new Result(true,"找回成功，请查看邮箱信息");
        }else {
            return new Result(false,"用户名与邮箱不匹配");
        }
    }
    @RequestMapping("/code")
    public Result code(@RequestBody Users user, HttpServletRequest request){
        Code code = new Code();
        yzm = code.yzm();
        HttpSession httpSession = request.getSession();
        httpSession.setAttribute("yzm",yzm);
        SendMail mySendMail = new SendMail();
        mySendMail.sendMail(user.getEmail(),"三丸医疗系统提醒，您的验证码为："+yzm+"。验证码有效期为5分钟");
        try {
            //TimerTask实现5分钟后从session中删除checkCode
            final Timer timer=new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    httpSession.removeAttribute("yzm");
                    System.out.println("code删除成功");
                    timer.cancel();
                }
            },5*60*1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true,"发送成功");
    }

}
